var api = require('../../utils/api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar:'',//用户头像
    user_nickname:'',//用户昵称
    times:0,//观看次数
    see_s: 0,//观看时间(秒)
    see_m: 0,//观看时间(分)
    h: 0,//累计时间(时)
    m: 0,//累计时间(分)
    experience:0,//观看经验
    Lv:0,//等级
    tong:0,//铜牌徽章
    yin: 0,//银牌徽章
    jin: 0,//金牌徽章
    days:0,//使用天数
    badge:[],//徽章列表
    login:false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = wx.getStorageSync('user');
    that.setData({
      id: user.id
    })
    // 获取徽章
    api.get({
      url: 'wxapp/product/getBadge',
      data: {
      },
      success: data => {
        wx.setStorageSync('badge', data.data);
        this.setData({
          badge: data.data
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var badge = wx.getStorageSync('badge');
    var that = this;
    api.post({
      url: 'wxapp/Index/my',
      data: {
        id: that.data.id,
      },
      success: data => {
        wx.setStorageSync('user', data.data);
        var user = wx.getStorageSync('user');
        console.log(data.data)

        let now = (Date.parse(new Date())) / 1000;
        let firstDay = data.data.create_time;
        let days = Math.ceil((now - firstDay)/3600/24)+1;
        console.log(firstDay)
        console.log(days)

        let s = parseInt(data.data.hours);// 秒
        let m = 0;//累计时间(分)
        let h = 0;//累计时间(时)
        let e = 0;//观看经验
        let f = parseInt(data.data.forward);//用户转发次数
        let Lv = 0;
        let tong = 0;//铜牌徽章
        let yin = 0;//银牌徽章
        let jin = 0;//金牌徽章

        let see_s = parseInt(data.timelong.timelong);
        let see_m = 0;//观看时间(分)

        let tong_badge = badge.tong_badge_num;//铜牌升银牌所需数量
        let yin_badge = badge.yin_badge_num;//银牌升金牌所需数量
        let badge_time = badge.badge_time;//升级所需观看时长
        let badge_transmit = badge.badge_transmit;//升级所需转发次数

        if (see_s > 59){
          see_m = parseInt(see_s / 60);
          see_s = parseInt(see_s % 60);
        }

        if (s > 59){
          m = parseInt(s / 60);
          s = parseInt(s % 60);
          if(m > 59){
            h = parseInt(m / 60);
            m = parseInt(m % 60);
            e = h;
            if (h > badge_time) {
              if (e > badge_time && parseInt(f) > badge_transmit){
                Lv = parseInt(h / badge_time) > parseInt(parseInt(f) / badge_transmit) ? parseInt(parseInt(f) / badge_transmit) : parseInt(h / badge_time)
                if(Lv > 0){
                  tong = Lv
                  if (tong >= tong_badge){
                    yin = parseInt(tong / tong_badge)
                    tong = parseInt(tong % tong_badge)
                    if (yin >= yin_badge){
                      jin = parseInt(yin / yin_badge)
                      yin = parseInt(yin % yin_badge)
                    }
                  }
                }
              }
            }
          }
        }

        // 测试使用
        // console.log(s);
        // console.log(m);
        // console.log(h);
        // console.log(tong);
        // console.log(yin);
        // console.log(jin);
        // console.log(badge_time);
        // console.log(badge_transmit);

        
        this.setData({
          user: data.data,
          avatar: data.data.avatar,
          user_nickname: data.data.user_nickname,
          times: data.data.times,
          h: h,
          m: m,
          experience: e,
          Lv: Lv,
          jin: jin,
          yin: yin,
          tong: tong,
          see_s: see_s,
          see_m: see_m,
          days: days-1,
          medal_type: data.data.medal_type,
          medal_time: data.data.medal_time,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onReady()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  //分享
  onShareAppMessage(e) {
    let user_id = this.data.id
    return {
      path: '/pages/index/index',
      success: function (res) {
        api.post({
          url: 'wxapp/Index/share',
          data: {
            title:'我的',
            user_id: user_id
          },
          success: res => {
            wx.showToast({
              title: res.msg,
              icon: 'success',
              duration: 2000
            })
          }
        })
      },
    }
  },

  //跳转徽章页面
  graduation(){
    let that = this;
    let days = that.data.days
    let hours = that.data.h
    let medal_type = that.data.medal_type
    let medal_time = that.data.medal_time
    if (medal_type != 0){
      wx.navigateTo({
        url: '/pages/graduation/graduation?days=' + days + '&hours=' + hours + '&medal_type=' + medal_type + '&medal_time=' + medal_time,
      })
    }
  },

//登录获取
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      api.login();
      var userInfo = e.detail.userInfo
      var setInt = setInterval(function () {
        if (userInfo) {
          clearInterval(setInt);
          that.setData({
            login: true,
            avatar: userInfo.avatarUrl,
            user_nickname: userInfo.nickName,
          })
        }
      }, 500);  
    }
  },
})